# Prepare WA returns
# Note: clean and rearrange 2020 WA returns
# By Dom Valentino and Chris Kenny
# libs --------------------------------------------------------------------
library(tidyverse) # select(), filter(), mutate(), etc.
library(haven)

# data ---------------------------------------------------------------
returns_dirty <- read.csv("../data/returns_wa/returns_wa.csv")
cvap <- read_dta("../data/modified data/county_cvap.dta") # read cvap data
turnout <- read.csv("../data/returns_wa/turnout_wa.csv")

# returns ---------------------------------------------------------------
returns <- returns_dirty %>%  mutate(Party = case_when(
      grepl("Democratic", Party) ~ "dem",
      grepl("Republican", Party) ~ "rep",
      T ~ Party), 
    Race = case_when(
      grepl("President", Race) ~ "pres",
      grepl("Governor", Race) & !grepl("Lt", Race) ~ "gov")) %>% 
  filter(Race %in% c("pres", "gov"), Party %in% c("dem", "rep")) %>% 
  unite(obs, Party, Race, sep = "_") %>% 
  select(County, obs, Votes) %>% 
  pivot_wider(names_from = obs, values_from = Votes) %>% 
  transmute(dem_share_pres = dem_pres / (dem_pres + rep_pres), dem_share_gov = dem_gov / (dem_gov + rep_gov), dem_share_sen = NA,
         fips = NA, treat = 1, state = "WA", year = 2020, county = County)

# merge
final_wa <- left_join(returns, turnout, by = "county") %>% 
  left_join(., cvap, by = c("state", "county", "year")) %>% 
  mutate(turnout_share = ballots_cast / cvap_approx)

# save
write_csv(final_wa, path = '../data/analysis/analysis_wa.csv')
